<template>
  <div>
    <h1>图书管理</h1>
    <div class="tool">
      <el-form label-width="80px" inline>
        <el-form-item label="书名">
          <el-input></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input></el-input>
        </el-form-item>
        <el-form-item label="出版社">
          <el-input></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info">查询</el-button>
          <el-button type="primary" @click="showAddForm">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 内容 -->
    <div class="content">
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="bookname" label="书名"> </el-table-column>
        <el-table-column prop="author" label="作者"> </el-table-column>
        <el-table-column label="数据创建者">
          <template slot-scope="scope">
            <!-- scope.row = 数组中的每一项 v-for item -->
            <!-- {{ scope.row.creator ? scope.row.creator : "无" }} -->
            <div v-if="scope.row.creator">{{ scope.row.creator }}</div>
            <div v-else>无</div>
          </template>
        </el-table-column>
        <el-table-column prop="publisher" label="出版社"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)"
              type="success"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <!-- 修改成表单 -->
      <el-form label-width="80px" :model="form" :rules="rules" ref="ruleForm">
        <el-form-item label="书名" prop="bookname">
          <el-input v-model="form.bookname"></el-input>
        </el-form-item>
        <el-form-item label="作者" prop="author">
          <el-input v-model="form.author"></el-input>
        </el-form-item>
        <el-form-item label="出版社" prop="publisher">
          <el-input v-model="form.publisher"></el-input>
        </el-form-item>
      </el-form>
      <!-- 修改成表单 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 导入 axios
import axios from "axios";

export default {
  name: "App",
  data() {
    return {
      tableData: [],
      // 控制对话框显示和隐藏的
      dialogVisible: false,
      // 新增的表单的数据
      form: {
        // 书名
        bookname: "",
        // 作者
        author: "",
        // 出版社
        publisher: "",
      },
      // 验证规则
      rules: {
        // trigger 失去焦点的时候开始验证
        bookname: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
        author: [
          { required: true, message: "请输入作者名称", trigger: "blur" },
        ],
        publisher: [
          { required: true, message: "请输入出版社名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 向接口 请求数据  axios
    async getList() {
      // 查询 get请求
      const result = await axios.get("http://ajax-api.itheima.net/api/books");
      console.log(result.data.data);
      // 给 数据赋值
      this.tableData = result.data.data;
    },
    // 显示新增表单
    showAddForm() {
      this.dialogVisible = true;
    },
    // 点击确定  执行新增的要触发
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 验证通过 执行
          // 发送 新增的请求  async 放在和await 最近的函数
          await axios.post("http://ajax-api.itheima.net/api/books", this.form);
          // 后端响应数据
          // 弹出恭喜提示
          this.$message.success("新增成功");
          // 关闭模态框
          this.dialogVisible = false;

          // 刷新列表数据
          this.getList();
          // 清空表单字段
          this.resetForm("ruleForm");
        } else {
          // 不通过 ..
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      // el-form的组件的实例 重置表单
      this.$refs[formName].resetFields();
    },
  },
  // vue组件 生命周期
  // vue实例创建完毕 时期
  created() {
    // 发送请求 获取数据  页面的渲染
    this.getList();
  },
};
</script>

<style></style>
